<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>图片幻灯片</title>
  <style>
    #slideshow-container {
        width: 500px;
        height: 300px;
        margin: 50px auto;
        position: relative;
    }

    .slide {
        width: 100%;
        height: 300px;
        position: absolute;
        top: 0;
        left: 0;
        opacity: 0;
        transition: opacity 0.5s ease-in-out;
    }

    .slide.active {
        opacity: 1;
    }
    .button{
        margin: auto;
        width: 500px;
        display: flex;
        justify-content: center;
    }
    button {
        width: 100px;
        height: 40px;
        
    }
  </style>
</head>

<body>
  <div id="slideshow-container">
    <img src="image/1.jpg" alt="图片1" class="slide">
    <img src="image/2.jpg" alt="图片2" class="slide" style="display: none;">
    <img src="image/3.jpg" alt="图片3" class="slide" style="display: none;">
    <img src="image/4.jpg" alt="图片3" class="slide" style="display: none;">
    <img src="image/5.jpg" alt="图片3" class="slide" style="display: none;">
    <img src="image/6.jpg" alt="图片3" class="slide" style="display: none;">
    <!-- 你可以在这里添加更多图片 -->
    </div>
    <div class="button">
    <button id="prevButton">上一张</button>
    <button id="nextButton">下一张</button>
    <button id="stopButton">停止播放</button>
    <button id="startButton">开始播放</button></div>
  <script>
      
    // 获取幻灯片相关的DOM元素
    const slideshowContainer = document.getElementById('slideshow-container');
    const slides = document.querySelectorAll('.slide');
    const prevButton = document.getElementById('prevButton');
    const nextButton = document.getElementById('nextButton');
    const stopButton = document.getElementById('stopButton');
    const startButton = document.getElementById('startButton');

    let currentSlideIndex = 0;
    let intervalId;

    // 显示指定索引的幻灯片
    function showSlide(index) {
    slides.forEach((slide, i) => {
        if (i === index) {
        slide.style.display = 'block';
        setTimeout(() => {
            slide.classList.add('active');
        }, 1);
        } else {
            slide.classList.remove('active');
            setTimeout(() => {
                slide.style.display = 'none';
            }, 1);
        }
    });
    }

    // 自动切换到下一张幻灯片的函数
    function nextSlide() {
        currentSlideIndex = (currentSlideIndex + 1) % slides.length;
        showSlide(currentSlideIndex);
    }

    // 切换到上一张幻灯片的函数
    function prevSlide() {
        currentSlideIndex = (currentSlideIndex - 1 + slides.length) % slides.length;
        showSlide(currentSlideIndex);
    }

    // 开始幻灯片播放
    function startSlideshow() {
        if (!intervalId) {
            intervalId = setInterval(nextSlide, 3000);
        }
    }

    // 停止幻灯片播放
    function stopSlideshow() {
        clearInterval(intervalId);
        intervalId = null;
    }

    // 初始化时显示第一张幻灯片
    function init() {
        showSlide(currentSlideIndex);
    }

    // 为上一张、下一张、停止、开始按钮添加点击事件监听器
    prevButton.addEventListener('click', prevSlide);
    nextButton.addEventListener('click', nextSlide);
    stopButton.addEventListener('click', stopSlideshow);
    startButton.addEventListener('click', startSlideshow);

    // 页面加载完成后初始化幻灯片
    window.addEventListener('load', init);

  </script>
</body>

</html>